package com.jxc.jxc01.yk.dao;

import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.entity.SalesReturn;
import com.jxc.jxc01.yk.util.JDBCConn;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SalesReturnDaoImpl implements ISalesReturnDao {

    /**
     * 增加销售退货清单
     */
    public boolean addSalesReturn(SalesReturn salesReturn) {
       ResultSet rs = null;
        try {
            //3.插入
            String sql = "insert into salesreturn(salesnumber,goods_returned_num,goods_returned_count,goods_returned_price," +
                    "goods_returned_total,goods_returned_time,goods_returned_reason,status,comment) values (?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, salesReturn.getSalesnumber());
            pstmt.setInt(2, salesReturn.getGoods_returned_num());
            pstmt.setInt(3, salesReturn.getGoods_returned_count());
            pstmt.setDouble(4, salesReturn.getGoods_returned_price());
            pstmt.setDouble(5, salesReturn.getGoods_returned_total());
            pstmt.setString(6, salesReturn.getGoods_returned_time());
            pstmt.setString(7, salesReturn.getGoods_returned_reason());
            pstmt.setString(8, salesReturn.getCondition());
            pstmt.setString(9, salesReturn.getComment());
            int rows = pstmt.executeUpdate();
            if (rows == 0) {
                //添加失败
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    /**
     * 查询销售退货清单
     */
    public List<SalesReturn> ListSalesReturn(PageHelper<SalesReturn> pageHelper) {
        //将查询到的销售清单信息存储集合中
        List<SalesReturn> salesReturns = new ArrayList<>();
        ResultSet rs = null;
        try {
            //3.查询出库表
            //分页查询
            String sql = "select count(*) total from salesreturn";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            rs = pstmt.executeQuery();
            //列表数据总数
            while (rs.next()){
                int total = rs.getInt("total");
                pageHelper.setTotal(total);
            }
            //总页数
            pageHelper.setPageCount(pageHelper.getTotal()%pageHelper.getPageSize()==0?
                    pageHelper.getTotal()/pageHelper.getPageSize():pageHelper.getTotal()/pageHelper.getPageSize()+1);
            //3.查询销售表
            sql = "select * from salesreturn limit ?,?";
             pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, (pageHelper.getPageNum()-1)*pageHelper.getPageSize());
            pstmt.setInt(2, pageHelper.getPageSize());
            rs = pstmt.executeQuery();
            while (rs.next()) {
                SalesReturn salesReturn = new SalesReturn();
                salesReturn.setId(rs.getInt(1));
                salesReturn.setSalesnumber(rs.getInt(2));
                salesReturn.setGoods_returned_num(rs.getInt(3));
                salesReturn.setGoods_returned_count(rs.getInt(4));
                salesReturn.setGoods_returned_price(rs.getDouble(5));
                salesReturn.setGoods_returned_total(rs.getDouble(6));
                salesReturn.setGoods_returned_time(rs.getString(7));
                salesReturn.setGoods_returned_reason(rs.getString(8));
                salesReturn.setCondition(rs.getString(9));
                salesReturn.setComment(rs.getString(10));
                salesReturns.add(salesReturn);
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return salesReturns;
    }

    /**
     * 根据销售单号查找某一条退货记录
     */
    public List<SalesReturn> findSalesReturnumber(int salesnumber) {
        //将查询到的销售清单信息存储集合中
        List<SalesReturn> salesReturns = new ArrayList<>();
        ResultSet rs = null;

        try {
            String sql = "select * from salesreturn where salesnumber=?";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, salesnumber);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                SalesReturn salesReturn = new SalesReturn();
                salesReturn.setId(rs.getInt(1));
                salesReturn.setSalesnumber(rs.getInt(2));
                salesReturn.setGoods_returned_num(rs.getInt(3));
                salesReturn.setGoods_returned_count(rs.getInt(4));
                salesReturn.setGoods_returned_price(rs.getDouble(5));
                salesReturn.setGoods_returned_total(rs.getDouble(6));
                salesReturn.setGoods_returned_time(rs.getString(7));
                salesReturn.setGoods_returned_reason(rs.getString(8));
                salesReturn.setCondition(rs.getString(9));
                salesReturn.setComment(rs.getString(10));
                salesReturns.add(salesReturn);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return salesReturns;
    }
}
